package com.tony.mywork.huju;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tony.mywork.huju.utils.HttpSendUtil;
import com.tony.mywork.huju.utils.Notify;

public class PullTicket {

	public final static Logger log = LoggerFactory.getLogger(PullTicket.class);
	
	public final static String url = "http://m.wenhuayun.cn/wechatVenue/venueWcActivity.do";
	public final static String params = "venueId=d1150c4cd00b4307aa1e2cd095072808&Lon=121.48&Lat=31.22&pageIndex=0&pageNum=20";
	public static String lastSendDay = "2018-07-07";
	public final static int maxSendTimes = 50;
	
	public static Map<String, Boolean> msgs = new HashMap<String, Boolean>();
	public static int sendCnt = 0;
	
	public static void main(String[] args) {
		lastSendDay = System.getProperty("lastSendDay");
		while (true) {
			try {
				log.info("lastsendDay:" + lastSendDay);
				String retJson = HttpSendUtil.sendPost(url, params);
				JSONObject jo = JSON.parseObject(retJson);
				JSONArray data = jo.getJSONArray("data");

				if (data == null) {
					log.info("列表为空");
					continue;
				}
				
				log.info("当前时间:" + new Date() + ",列表总数:" + data.size() + ", lastSendDay:" + lastSendDay + ", sendCnt:" + sendCnt + ", maxSendTimes:" + maxSendTimes);

				if (data.size() > 0) {
					 for (int i = 0; i < data.size(); i++) {
						 JSONObject element = data.getJSONObject(i);
						 String activityName = element.getString("activityName");
						 String activityStartTime = element.getString("activityStartTime");
						 log.info("戏剧名称:" + activityName + ", 时间:" + activityStartTime);
						 
						 if (activityStartTime.compareTo(lastSendDay) > 0) {
							 if (sendCnt < maxSendTimes) {
								 log.info("【发送消息到手机】:" + activityName + " ," + activityStartTime);
								 Notify.sendMsg("18016290912", activityName + " ," + activityStartTime);
								 Notify.sendMsg("15821700024", activityName + " ," + activityStartTime);
								 Notify.sendMsg("18917281520", activityName + " ," + activityStartTime);
								 Notify.sendMsg("13661871629", activityName + " ," + activityStartTime);
								 sendCnt++;
							 } else {
								 log.info("【发送次数超限】");
							 }
							 lastSendDay = activityStartTime;
						 }
					 }
				} else {
					continue;
				}

				Thread.sleep(60000);
			} catch (Exception e) {
				log.error("发生错误", e);
			}
		}
	}

}
